package ran.entity;

import lombok.Data;
import ran.enums.ResultCode;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ran
 * Date: 2025-10-03
 * Time: 15:26
 */
@Data
public class Result<T> {
    private int code;
    private String msg;
    private T data;

    public static <T> Result<T>  ok() {
        return packageResult(null,ResultCode.SUCCESS);
    }
    public static <T> Result<T> ok(T data) {
        return packageResult(data,ResultCode.SUCCESS);
    }

    public static <T> Result<T> fail() {
        return packageResult(null,ResultCode.FAILED);
    }
    public static <T> Result<T> fail(String msg) {
        return assembleResult(2000,msg,null);
    }

    public static <T> Result<T> fail(ResultCode resultCode) {
        return packageResult(null,resultCode);
    }

    private static <T> Result<T> packageResult(T data, ResultCode resultCode) {
        Result result = new Result();
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMsg());
        result.setData(data);
        return  result;
    }

    public static <T> Result<T> fail(int code, String msg) {
        return assembleResult(code, msg, null);
    }
    private static <T> Result<T> assembleResult(int code, String msg, T data) {
        Result<T> r = new Result<>();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }
}
